import { list as listBaiJia, total } from '../services/warehouse';
import { list as listArticle } from '../services/article';
import { list as listCategory } from '../services/category';

export default {
  namespace: 'warehouse',

  state: {
    baiJiaList: [],
    categoryList: [],
    articleList: [],
    baiJiaTotal: 0,
    articleTotal: 0,
    total: 0,
  },

  effects: {
    *listBaiJia({ payload }, { call, put }) {
      const response = yield call(listBaiJia, payload);
      yield put({
        type: 'loadBaiJiaList',
        payload: response.data,
      });
    },
    *total(_, { call, put }){
      const response = yield call(total);
      yield put({
        type: 'loadTotal',
        payload: response.data,
      })
    },
    *listArticle({ payload }, { call, put }) {
      const response = yield call(listArticle, payload);
      yield put({
        type: 'loadArticleList',
        payload: response.data,
      });
    },
    *listCategory(_, { call, put }) {
      const response = yield call(listCategory);
      yield put({
        type: 'loadCategoryList',
        payload: response.data,
      });
    },
  },

  reducers: {
    loadBaiJiaList(state, action) {
      return {
        ...state,
        baiJiaList: action.payload.list,
        baiJiaTotal: action.payload.total,
      };
    },
    loadTotal(state, action){
      return {
        ...state,
        total: action.payload.total,
      }
    },
    loadArticleList(state, action) {
      return {
        ...state,
        articleList: action.payload.list,
        articleTotal: action.payload.total,
      };
    },
    loadCategoryList(state, action) {
      return {
        ...state,
        categoryList: action.payload,
      };
    },
  },
};
